package com.study.runnable;


import org.apache.commons.lang.StringUtils;

public class raceRunnable implements Runnable {
    private static String winer;
    public void run() {
        for (int i = 0; i <= 100; i++) {
            //让兔子休眠
            if ("兔子".equalsIgnoreCase(Thread.currentThread().getName()) && i%10 == 0){
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            //判断比赛是否结束
            boolean flag = gameOver(i);
            if (flag) {
                break;
            }
            System.out.println(Thread.currentThread().getName() + "跑了"+i+"步");
        }
    }

    private static Boolean gameOver(int steps) {
        // 判断是否已经存在胜利者 存在就返回true 结束比赛
        if (StringUtils.isNotEmpty(winer)) {
            return true;
        }
        {
            if (steps >= 100) {
                winer = Thread.currentThread().getName();
                System.out.println("获胜者是--->" + winer);
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        raceRunnable runnable = new raceRunnable();
        new Thread(runnable,"兔子").start();
        new Thread(runnable,"乌龟").start();
    }
}

